<!--  -->
<template>
  <div class="index container">
    <div class="topBox">
      <el-input size="medium" v-model="projectInfo" placeholder="请输入内容" class="projectInfoInput topInput"></el-input>
      <el-button type="primary" size="medium" icon="el-icon-search">搜索</el-button>
      <el-button
        type="primary"
        size="medium"
        icon="el-icon-circle-plus-outline"
        @click="diagShow(1,'add')"
      >新增
      </el-button>
    </div>
    <div class="contain">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="project_id" label="ID" width="60"></el-table-column>
        <el-table-column prop="serial" label="项目编号" width="180"></el-table-column>
        <el-table-column prop="decree" label="项目令号" width="180"></el-table-column>
        <el-table-column prop="title" label="项目名称"></el-table-column>
        <!--          <el-table-column prop="introduce" label="项目介绍"></el-table-column>-->
        <el-table-column prop="head_user" label="项目负责人" width="120"></el-table-column>
        <el-table-column prop="start_date" label="开始时间" width="130"></el-table-column>
        <el-table-column prop="end_date" label="结束时间" width="130"></el-table-column>
        <el-table-column prop="total_number" label="总金额" width="120"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="diagViewShow(2,'view',scope.row)" type="text" size="small">查看</el-button>
            <el-button type="text" @click="diagShow(3,'edit',scope.row)" size="small">编辑</el-button>
            <el-button type="text" @click="deleteListItemFn(scope.row.project_id)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :page-size="pagination.size"
        :current-page="pagination.page"
        layout="pager"
        @current-change="currentChange"
        :page-count="pagination.total_page">
      </el-pagination>
    </div>
    <el-dialog :title="diagName" :visible.sync="dialogFormVisible" class="diagForm">
      <el-form :model="diagForm" :rules="dialogRules" ref="diagForm">
        <el-form-item label="项目ID" :label-width="formLabelWidth" v-if="isEdit">
          <el-input v-model="diagForm.project_id" autocomplete="off" v-bind:disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="项目编号" :label-width="formLabelWidth">
          <el-input v-model="diagForm.serial" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目令号" :label-width="formLabelWidth">
          <el-input v-model="diagForm.decree" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="diagForm.title" autocomplete="off" width="120"></el-input>
        </el-form-item>
        <el-form-item label="项目介绍" :label-width="formLabelWidth" prop="introduction">
          <el-input type="textarea" v-model="diagForm.introduce" :rows="2" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="项目周期" :label-width="formLabelWidth" prop="dataRange">
          <el-date-picker
            v-model="dataRange"
            type="daterange"
            value-format="yyyy-MM-DD"
            format="yyyy-MM-DD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="总绩效" :label-width="formLabelWidth" prop="total_number">
          <el-input v-model="diagForm.total_number" autocomplete="off"></el-input>
          元
        </el-form-item>
        <el-form-item label="负责人" :label-width="formLabelWidth" prop="head_user_id">
          <el-select v-model="diagForm.head_user_id" clearable filterable placeholder="请选择">
            <el-option v-for="item in userList" :key="item.nickname" :label="item.nickname"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="projectSaveFn('diagForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="diagName" :visible.sync="dialogViewFormVisible" class="diagForm">
      <div class="contain">
        <el-table :data="userOfProjectList" style="width:calc(100% - 10px);margin:auto;">
          <el-table-column prop="user_id" label="用户编号"></el-table-column>
          <el-table-column prop="nickname" label="姓名"></el-table-column>
          <el-table-column prop="performance" label="新签合同"></el-table-column>
          <el-table-column prop="achievement" label="当前到款"></el-table-column>
          <el-table-column prop="argument" label="论证编制"></el-table-column>
          <el-table-column prop="total" label="总计"></el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogViewFormVisible = false">关 闭</el-button>
        <!--        <el-button type="primary" @click="projectSaveFn()">确 定</el-button>-->
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import {
    getPrjList,
    getUserList,
    getIndexData,
    deletePrj,
    projectSave,
    getPerformanceList
  } from "@/api/api.js";
  import {getAchievementList, getArgumentList, getArgumentListByProject, getPerformancesOfProject} from "../../api/api";

  export default {
    components: {},
    data() {
      return {
        projectInfo: "",
        tableData: null,
        dialogFormVisible: false,
        dialogViewFormVisible: false,
        isEdit: false,
        dataRange: ["", ""],
        pagination: {
          page: 1,
          size: 10,
          total: 20,
          total_page: null,
        },
        userParams: {
          page: 1,
          size: 10
        },
        diagForm: {
          project_id: "",
          title: "",
          serial: "",
          decree: "",
          introduce: "",
          start_date: "",
          end_date: "",
          total_number: 0,
          head_user_id: ""
        },
        diagFormdisabled: true,

        formLabelWidth: "80px",
        userList: null,
        userOfProjectList: null,
        diagName: "提示",
        performance: {
          project_id: "",
          type: 1,
          list: []
        },
        dialogRules: {
          // title: [
          //   {required: true, message: '请输入项目标题', trigger: 'change'}
          // ],
          // introduction: [
          //   {required: true, message: '请输入项目介绍', trigger: 'change'}
          // ],
          // dataRange: [
          //   {required: true, message: '请选择项目周期', trigger: 'change'}
          // ],
          total_number: [
            {required: true, message: '请输入项目总绩效', trigger: 'change'}
          ],
          // head_user_id: [
          //   {required: true, message: '请选择项目负责人', trigger: 'change'}
          // ],
        },
      };
    },
    computed: {},
    watch: {},
    created() {
      this.getPrjListFn();
      // // this.getIndexDataFn();
      this.getUserListFn();
    },
    mounted() {
    },
    methods: {
      async getPrjListFn() {
        let data = await getPrjList(this.pagination);
        if (data.data.code === 200) {
          console.log(data.data.data.list)
          this.tableData = data.data.data.list
          this.pagination.total_page = data.data.data.total_page
        }
        // this.tableData = data.data;
        // console.log(this.tableData)
      },
      async getUserListFn() {
        let data = await getUserList(this.userParams);
        if (data.data.code === 200) {
          this.userList = data.data.data.list;
        }
      },
      async getPerformancesOfProjectFunc(prjId) {
        console.log(prjId)
        let params = {
          project_id: prjId
        }
        let data = await getPerformancesOfProject(params);
        console.log(data.data.data)
        if (data.data.data === 'success') {
          this.userOfProjectList = []
        }
        this.userOfProjectList = data.data.data

      },
      async projectSaveFn(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            this.dialogFormVisible = false;
            let params = {};
            params['project_id'] = parseInt(this.diagForm.project_id);
            params['serial'] = this.diagForm.serial;
            params['decree'] = this.diagForm.decree;
            params['title'] = this.diagForm.title;
            params['introduce'] = this.diagForm.introduce;
            params['user_id'] = parseInt(this.diagForm.head_user_id);
            params['total_num'] = parseFloat(this.diagForm.total_number);
            params['start_date'] = new Date(this.dataRange[0]);
            params['end_date'] = new Date(this.dataRange[1]);
            console.log(params);
            let data = await projectSave(params);
            if (data.data.code === 200) {
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.dialogFormVisible = false;
              this.getPrjListFn();
            } else {
              this.$message.error('添加失败');
            }
          } else {
            this.$message.error('添加失败');
            return false;
          }
        });
      },
      addPerformanceItem() {
        this.performance.list.push({user_id: null, performance: 0});
      },
      deletePerformanceItem(index) {
        this.performance.list.splice(index, 1);
      },
      diagViewShow(type, title, row) {
        this.dialogViewFormVisible = true
        this.diagName = '查看'
        this.getPerformancesOfProjectFunc(row.project_id)
      },
      diagShow(type, title, row) {
        if (title === 'view') {
          this.diagName = '查看'
          this.isEdit = false
        }
        if (title === 'edit') {
          this.diagName = '编辑'
          this.isEdit = true
        }
        if (title === 'add') {
          this.diagName = '添加'
          this.isEdit = false
        }

        this.dialogFormVisible = true;
        console.log(row)
        if (row) {
          this.diagForm = row;
          this.dataRange = [];
          this.dataRange.push(row.start_date);
          this.dataRange.push(row.end_date);
          // this.getPerformancesOfProjectFunc(row.project_id)
          // this.getPerformanceListFn(row.project_id)
          // this.getAchievementListFn(row.project_id)
          // this.getArgumentListFn(row.project_id)
        }
      },
      deleteListItemFn(prjId) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.deleteListItem(prjId);
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      async deleteListItem(prjId) {
        let data = await deletePrj({project_id: prjId})
        if (data.data.code === 200) {
          this.$message({
            type: "success",
            message: "删除成功"
          });
          this.getPrjListFn();
        } else {
          this.$message.error('添加失败');
        }
      },
      currentChange(page) {
        this.pagination.page = page;
        this.getPrjListFn()
      }
    }
  };
</script>

<style>
  .el-table td .el-table th {
    text-align: center;
  }
</style>

<style scoped lang="scss">
  .topBox {
    padding: 10px 0;
    width: calc(100% - 10px);
    margin: auto;
    border-bottom: solid 1px #e6e6e6;

    .topInput {
      width: 200px;
    }
  }

  .diagForm {
    .el-form-item {
      width: 450px;
    }

    .el-input {
      width: 350px;
    }
  }
</style>
